//logs.js
const app = getApp()
const CONFIG = require('../../config.js')

Page({
	data: {
		isShowBtn: false
	},

	formSubmit(e) {
		var loginInfo = wx.getStorageSync('loginInfo') || '';
		loginInfo = JSON.parse(loginInfo);
		let params = e.detail.value;
		params.loginType = 'phone';
		params.openId = loginInfo.openid;
		console.log(params,111)
        wx.request({
          url: CONFIG.host + "loginWeiXin",
          data: params,
          method: "POST",
          header: {
            'content-type': 'application/json',
          },
          success: res => {
			if(res.data.code === 200){
				let token = 'Bearer ' + res.data.token;
				let loginData = {
					openid : loginInfo.openid,
					token : token,
				}
				wx.setStorageSync('loginInfo', JSON.stringify(loginData));
				this.getInfoFn(token);
			}else {
				wx.showToast({
					title: res.data.msg,
					icon: 'none',
					duration: 2000
				})
			}
          },fail: function (error) {
            console.log(error);
          }
        })
	},
	// 获取用户详情
	getInfoFn(token){
        wx.request({
			url: CONFIG.host + "getInfo",
			data: {},
			method: "GET",
			header: {
			  'content-type': 'application/json',
			  'Authorization': token
			},
			success: res => {
				if(res.data.code === 200){
					wx.setStorageSync('userInfo', JSON.stringify(res.data.user))
					wx.switchTab({
						url: '../index/index'
					})
				}
			},fail: function (error) {
			  console.log(error);
			}
		  })
	},

	// 跳转到 注册页
	registerFn(){
		wx.redirectTo({
			url: '/pages/register/register'
		  })
	},

	formReset(e) {
		this.setData({
			chosen: ''
		})
	},

	onLoad: function () {
		if(app.globalData.loginInfo){
			app.decodeOpenIdCallback = res =>{
				
			}
		  } else {
			app.decodeOpenIdCallback = res =>{
			  if(res.token){
				  wx.switchTab({
					  url: '../index/index'
				  })
			  }
			}
		}
	}
})
